package com.jtxy.npms.utils;

import com.jtxy.npms.entity.Supervisor;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {
//    读取文件
public static Map readObject(String fileName){
    Map map=new HashMap();
    File file = new File(fileName);
    try {
        if (!file.exists()) {
           return map;
        }
        FileInputStream fis=new FileInputStream(fileName);
        ObjectInputStream ois=new ObjectInputStream(fis);
        map= (Map<String, Supervisor>) ois.readObject();

    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    return map;
    }

//    保存文件
    public static void writeObject(String fileName, Map object){
        //        将map集合写入到文件中
        File file = new File(fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
                System.out.println("创建文件成功。");
            }
//            创建文件输出流
            FileOutputStream out = new FileOutputStream(file);
//            创建对象输出流
            ObjectOutputStream objOut = new ObjectOutputStream(out);
//            将map集合写入到文件中
            objOut.writeObject(object);
//            刷新缓冲区
            objOut.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
